var app = getApp();
var getSign = require('../../utils/getSign/util.js')
Page({
  data: {
    radioItems: [
      { name: '企业用户', value: 1, checked: false},
      { name: '个人用户', value: 2, checked: true }
    ],
    hidden: false,
    selectAllStatus: true,
    index:2,
    invoiceId:0,
    dutyParagraph:'', 
    invoiceTitle:'', 
    invoiceType:1,  
    invoiceValue:'', 
    isDefault:1   
  },
  changeinvoiceTitle: function (e){
    var value = e.detail.value
    this.setData({
      invoiceTitle: value
    })
  },
  changedutyParagraph: function (e) {
    var value = e.detail.value
    this.setData({
      dutyParagraph: value
    })
  },
  changeinvoiceValue: function (e) {
    var value = e.detail.value
    this.setData({
      invoiceValue: value
    })
  },
  submitfapiao:function(){

    if (this.data.invoiceType == 1){
      if (this.data.invoiceTitle == ''){
        wx.showToast({
          title: '请输入抬头',
          duration:2000
        })
      }else{
        wx.showToast({
          title: '保存成功',
          duration:2000
        })
      this.requestPost('020', this.data.invoiceId, this.data.dutyParagraph, this.data.invoiceTitle
        , this.data.invoiceType, this.data.invoiceValue, this.data.isDefault)
      }
    } else if (this.data.invoiceType == 2){
      if (this.data.invoiceTitle == '') {
        wx.showToast({
          title: '请输入抬头',
          duration: 2000
        })
      } else if (this.data.dutyParagraph == '') {
        wx.showToast({
          title: '请输入税号',
          duration: 2000
        })
      } else if (this.data.invoiceValue == '') {
        wx.showToast({
          title: '请输入完整抬头',
          duration: 2000
        })
      } else {
        wx.showToast({
          title: '保存成功',
          duration: 2000
        })
  this.requestPost('020', this.data.invoiceId, this.data.dutyParagraph, this.data.invoiceTitle
          , this.data.invoiceType, this.data.invoiceValue, this.data.isDefault)  
      }
    }
  },
  onLoad: function (options) {
    //检查Session_key是否过期
    wx.checkSession({
      success: (res) => {
        //未过期
        console.log('session_key 发票未过期')
        //是否有数据
        if (true) {
          if (options.id) {
            this.setData({
              invoiceId: options.id
            })
          //没有数据 请求(apitype, pageIndex, classId, keys)
            var memberInfo = wx.getStorageSync('memberInfo')
            var postArgs = {
              'Api_Type': '019',
              'invoice_Id': options.id,
              'Member_Id': memberInfo.MemberId,
              'Member_Token': memberInfo.MemberToken
            }
            //获取
            getSign.request('https://wx-app-api.sjune.cn/yuren/API.ashx', 'POST', postArgs
              , '').then(
                (res) => {
                  console.log(res, '发票信息')
                  if (res.Status == 2) {
                    wx.clearStorage({
                      success: () => {
                        wx.switchTab({
                          url: '/pages/index/index',
                        })
                      }
                    })
                  } else if (res.Status == 0) {
                    this.setData({
                      dutyParagraph: res.Data.Duty_Paragraph,
                      invoiceTitle: res.Data.Invoice_Title,
                      invoiceType: res.Data.Invoice_Type,
                      invoiceValue: res.Data.Invoice_Value,
                      selectAllStatus: res.Data.Is_Default,
                    })
                    if (res.Data.Is_Default) {
                      this.setData({
                        isDefault: 1,
                      })
                    } else {
                      this.setData({
                        isDefault: 0,
                      })
                    }
                    var info = []
                    if (res.Data.Invoice_Type == 2) {
                      info = [
                        { name: '企业用户', value: 1, checked: true },
                        { name: '个人用户', value: 2, checked: false }
                      ]
                    } else if (res.Data.Invoice_Type == 1) {
                      info = [
                        { name: '企业用户', value: 1, checked: false },
                        { name: '个人用户', value: 2, checked: true }
                      ]
                    }
                    this.setData({
                      radioItems: info
                    })
                  }
                  
                },
                (err) => {
                  console.log(err.Message, '信息失败')
                }
              )
          }
        }
      },
      fail: (res) => {
        //过期
        wx.clearStorage({
          success: () => {
            wx.switchTab({
              url: '/pages/index/index',
            })
          }
        })
      }
    })
  },
  //发送请求
  requestPost: function (apitype, invoiceId, dutyParagraph, invoiceTitle, invoiceType, invoiceValue,isDefault) {
    var memberInfo = wx.getStorageSync('memberInfo')
    var postArgs = {
      'Api_Type': apitype,
      'invoice_Id': invoiceId,
      'Duty_Paragraph': dutyParagraph,
      'Invoice_Title': invoiceTitle,
      'Invoice_Type': invoiceType,
      'Invoice_Value': invoiceValue,
      'Is_Default': isDefault,
      'Member_Id': memberInfo.MemberId,
      'Member_Token': memberInfo.MemberToken
    }
    //获取
    getSign.request('https://wx-app-api.sjune.cn/yuren/API.ashx', 'POST', postArgs
      , '').then(
        (res) => {
          console.log(res, '修改或添加发票')
          if (res.Status == 2) {
            wx.clearStorage({
              success: () => {
                wx.switchTab({
                  url: '/pages/index/index',
                })
              }
            })
          } else if (res.Status == 0) {
            wx.showToast({
              title: '保存成功',
              duration: 2000,
              success: () => {
                setTimeout(() => {
                  wx.navigateBack({
                    delta: 1
                  })
                }, 2000)
              }
            })
          }
         
        },
        (err) => {
          console.log(err.Message, '产品列表信息失败')
        }
      )
  },

  radioChange: function (e) {
    var checked = e.detail.value
    var items = this.data.radioItems;
    if(checked==1)
    {
      items[0].checked=true
      items[1].checked=false
      this.setData({
        radioItems:items,
        invoiceType:2,
        isDefault:1,
        dutyParagraph: '',
        invoiceTitle: '',
        invoiceValue: ''
      })
    }else{
      items[0].checked = false
      items[1].checked = true
      this.setData({
        radioItems: items,
        invoiceType: 1,
        isDefault: 1,
        dutyParagraph: '',
        invoiceTitle: '',
        invoiceValue: ''
      })
    }
  },
  checkAll:function(e){
    if (this.data.isDefault == 1){
      this.setData({
        isDefault:0
      })
      console.log(this.data.isDefault)
    } else if (this.data.isDefault == 0){
      this.setData({
        isDefault: 1 
      })
      console.log(this.data.isDefault)
    }
  }
})